import pymysql
from tutorial import items


def getCon():
    connection = pymysql.connect(host='127.0.0.1', user='root', password='h303567469', db='FD', charset='utf8',
                                 cursorclass=pymysql.cursors.DictCursor)
    return connection


def label_is_exist():
    sql = """select count(*) from label where label.lab= "5G";"""

    try:
        with getCon().cursor() as cursor:
            cursor.execute(sql)
            results = cursor.fetchall()
            return True if results[0]['count(*)'] >= 1 else False
    except:
        print("error: select label count(*)")


def insert_label(label):
    sql = "insert into `label` (`lab`,`rec_time`) values('%s','%s');" % (label['lab'], label['rec_time'])
    cont = getCon()
    try:
        with cont.cursor() as cursor:
            cursor.execute(sql)
            cont.commit()
    except:
        print("error: insert_label ")
        cont.rollback()


def insert_tp_item(item):
    sql = "insert into `tingpan` (`code`,`name`,`full_time`,`full_res`) values('%s','%s','%s','%s')" % (
        tpit['code'], tpit['name'], tpit['full_time'], tpit['full_res'])
    # print(sql)

    cont = getCon()
    try:
        with cont.cursor() as cursor:
            cursor.execute(sql)
            cont.commit()
    except:
        print("error: insert tp_item ")
        cont.rollback()


if __name__ == "__main__":
    print(label_is_exist())
    # label = items.label()
    # label['lab'] = '5G'
    # label['rec_time'] = '2018-4-25'
    # insert_label(label)
    tpit = items.TP_item()
    # tpit['code'] = '023214'
    # tpit['name'] = '华为概念'
    # tpit['full_time'] = '2019-01-01'
    # tpit['full_res'] = '5G,华为,概念'
    # insert_tp_item(tpit)
    # id = scrapy.Field()
    # code = scrapy.Field()
    # name = scrapy.Field()
    # full_time = scrapy.Field()
    # full_res = scrapy.Field()
    #
    # insert_tp_item()
